package app.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import app.dao.UserDAO;

@WebServlet(name = "login", urlPatterns = { "/login" })
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public Login() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String remember = request.getParameter("checkboxRememberMe");
		
		System.out.println("value checkbox: " + remember);

		if (UserDAO.auth(username, password)) {
			System.out.println("auth success");
			HttpSession session = request.getSession();
			session.setAttribute("username", username);
			request.getRequestDispatcher("/create_card.jsp").forward(request, response);
		} else {
			System.out.println("authentication failed");
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		}
		
	}

}
